package com.ccl.keyassistant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.ccl.keyassistant.MainActivity;
import com.ccl.keyassistant.R;
import com.ccl.keyassistant.adapter.GuideViewPagerAdapyer;
import com.ccl.keyassistant.view.CustomDialog;

import java.util.ArrayList;
import java.util.List;

/*
 *项目名     keyassistant
 *包名       com.ccl.keyassistant.activity
 *文件名     GuideActivity
 *创建者     ccl
 *创建时间   2019-03-07 16:23
 *注解       引导页
 */

public class GuideActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, View.OnClickListener {

    //引导页
    private View view1;
    private View view2;
    private View view3;

    //引导页存放
    private List<View> mGuideViewList;

    private ViewPager mViewPager;
    private List<View> mDatas;
    private LinearLayout mGuideLL;
    private ImageButton mImageBtn;
    private Button mBtn;
    //记录上一个小圆点的位置
    private int oldPointCount;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);

        initView();
        initData();

        //设置数据适配器
        mViewPager.setAdapter(new GuideViewPagerAdapyer(mDatas));
        //设置监听事件
        mViewPager.addOnPageChangeListener(this);
    }

    //初始化view
    private void initView() {

        mViewPager = (ViewPager) findViewById(R.id.guide_vp);
        mGuideLL = (LinearLayout) findViewById(R.id.guide_ll);
        //默认不显示
        mImageBtn = (ImageButton) findViewById(R.id.guide_ibtn);
        mImageBtn.setOnClickListener(this);
        //默认显示
        mBtn = (Button) findViewById(R.id.guide_btn);
        mBtn.setOnClickListener(this);

        view1 = View.inflate(this, R.layout.guide_view_one, null);
        view2 = View.inflate(this, R.layout.guide_view_two, null);
        view3 = View.inflate(this, R.layout.guide_view_three, null);

    }

    //初始化数据
    private void initData() {
        mDatas = new ArrayList<>();
        mDatas.add(view1);
        mDatas.add(view2);
        mDatas.add(view3);

        //准备小圆点
        View point;
        for (int i = 0; i < mDatas.size(); i++) {
            point = new View(this);
            point.setBackgroundResource(R.drawable.guide_point);
            //默认设置为非选中状态
            point.setEnabled(false);
            //添加到LinearLayout布局中，设置每个小圆点距左边10dp
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(15, 15);
            params.leftMargin = 10;
            mGuideLL.addView(point, params);
        }

    }

    //切换页面时调用该方法
    //当页面切换时设置小圆点的状态，以及控制控件的显示与否
    @Override
    public void onPageSelected(int position) {

        switch (position) {
            case 0:
            case 1:
                mImageBtn.setVisibility(View.VISIBLE);
                mBtn.setVisibility(View.GONE);
                break;
            case 2:
                mBtn.setVisibility(View.VISIBLE);
                mImageBtn.setVisibility(View.GONE);
                break;
        }

        mGuideLL.getChildAt(oldPointCount).setEnabled(false);
        mGuideLL.getChildAt(position).setEnabled(true);
        oldPointCount = position;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    //跳过和进入主页，进入指纹识别的点击事件
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.guide_btn:
            case R.id.guide_ibtn:
                startActivity(new Intent(GuideActivity.this, LoginActivity.class));
                finish();
                break;
        }
    }

    //重写返回，不让其生效

    @Override
    public void onBackPressed() {

        return;
    }
}
